package personal.lijun.skeleton.springcloud.serviceprovider.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.data.Event;
import personal.lijun.skeleton.spring.springcloud.serviceprovider.service.EventsService;
import personal.lijun.skeleton.springcloud.common.data.ApiResultPrimitiveValue;
import personal.lijun.skeleton.springcloud.dataaccess.dao.EventsMapper;
import personal.lijun.skeleton.springcloud.dataaccess.data.Events;
import personal.lijun.skeleton.springcloud.dataaccess.data.EventsExample;
import personal.lijun.skeleton.springcloud.utils.validate.ParamValid;
import personal.lijun.skeleton.springcloud.utils.validate.ParamValidException;
import personal.lijun.skeleton.springcloud.utils.validate.ValidGroups;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: lijun@sstcsoft.com
 * @Date: 2019/9/16 10:01
 */
@Service("EventsService")
public class EventsServiceImp implements EventsService {

    @Resource
    private EventsMapper eventsMapper;
    /**
     * 保存用户事件
     *
     * @param event 用户信息
     */
    @Override
    @Transactional
    public Event save(Event event) {
        if(event.getId()==null){
            addNewEvents(event);
        }
        else {
            updateEvents(event);
        }
        return event;
    }

    /**
     * 增加一个新用户事件
     *
     * @param record
     * @return
     */
    @Override
    @ParamValid
    public Event addNewEvents(@RequestBody Event record) {
        Events events = new Events();
        BeanUtils.copyProperties(record,events);
        eventsMapper.insert(events);
        record.setId(events.getId());
        return record;
    }

    /**
     * 更新指定用户事件信息
     *
     * @param record
     * @return
     */
    @Override
    @ParamValid
    public Event updateEvents(@RequestBody Event record) {
        Events events = new Events();
        BeanUtils.copyProperties(record,events);
        eventsMapper.updateByPrimaryKeySelective(events);
        return record;
    }

    /**
     * 根据id删除指定用户事件
     *
     * @param id 用户事件id
     * @return
     */
    @Override
    public Integer deleteEventsById(Long id) {
        if(id == null)
            throw new ParamValidException("id 不为空！");
        return eventsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询指定id的用户事件信息
     *
     * @param id 用户事件id
     * @return
     */
    @Override
    public Event  getEventsById(Long id) {
        if(id == null)
            throw new ParamValidException("id 不为空！");
        Events events = eventsMapper.selectByPrimaryKey(id);
        Event event = new Event();
        BeanUtils.copyProperties(events,event);
        return event;
    }

    /**
     * 查询指定用户的事件
     *
     * @param userId 用户id
     * @return 事件列表
     */
    @Override
    public List<Event> findForUser(long userId) {
        EventsExample eventsExample = new EventsExample();
        EventsExample.Criteria criteria = eventsExample.createCriteria();
        criteria.andOwnerEqualTo(userId);
        List<Events> eventsList = eventsMapper.selectByExample(eventsExample);
        List<Event> retList = new ArrayList<>(eventsList.size());
        Event event;
        for(Events events : eventsList){
            event = new Event();
            BeanUtils.copyProperties(events,event);
            retList.add(event);
        }
        return retList;
    }

    @Override
    public List<Event> getEvents(){
        List<Events> eventsList = eventsMapper.selectByExample(null);
        List<Event> retList = new ArrayList<>(eventsList.size());
        Event event;
        for(int i=0;i<eventsList.size();i++  ){
            event = new Event();
            BeanUtils.copyProperties(eventsList.get(i),event);
            retList.add(event);
            eventsList.set(i,null);
        }
        return retList;
    }
}
